package com.oocl.crm.repository.impl;

import com.oocl.crm.customer.Customer;
import com.oocl.crm.db.MyLinkedList;
import com.oocl.crm.db.MyLinkedListFactory;
import com.oocl.crm.db.MyLinkedListTest;
import com.oocl.crm.repository.CustomerDataProcess;
import com.oocl.crm.util.FormatPrinter;

public class CustomerDataProcessImpl implements CustomerDataProcess <Customer>{
	private MyLinkedList<Customer> db;
	public CustomerDataProcessImpl(MyLinkedList<Customer> db) {
		this.db=db;
	}
	@Override
	public void addCustomer(Customer c) {
		// TODO Auto-generated method stub
		db.addNode(c);
	}

	@Override
	public void updateCustomer(String id,Customer c) {
		// TODO Auto-generated method stub
		Customer target=findCustomerById(id);
		if(target!=null)db.updateNode(target, c);
		else FormatPrinter.printNoSuchCustomer(null);
		
	}

	@Override
	public void deleteCustomer(String id) {
		// TODO Auto-generated method stub
		Customer target=findCustomerById(id);
		if(target!=null)db.deleteNode(target);
		else FormatPrinter.printNoSuchCustomer(null);
	}
	
	@Override
	public Customer[] find(String[] condtion) {
		MyLinkedList<Customer>.Node<Customer> node=db.getHead();
		Customer[] view = new Customer[db.getSize()];
		String[] strSplite;
		boolean flag;
		int l=0;
		while(node.getNext()!=null){
			flag=true;
			for (int i = 0; i < condtion.length; i++) {
				strSplite=condtion[i].split(":");
				switch (strSplite[0]) {
				case "id":
					if(node.getNext().getData().getId().compareTo(strSplite[1])!=0)flag=false;
					break;
				case "name":
					if(node.getNext().getData().getName().compareTo(strSplite[1])!=0)flag=false;
					break;
				case "address":
					if(node.getNext().getData().getAddress().compareTo(strSplite[1])!=0)flag=false;
					break;
				case "sex":
					if(node.getNext().getData().getSex().compareTo(strSplite[1])!=0)flag=false;
					break;
				case "call":
					if(node.getNext().getData().getCall().compareTo(strSplite[1])!=0)flag=false;
					break;
				case "birthday":
					if(node.getNext().getData().getBirthday().compareTo(strSplite[1])!=0)flag=false;
					break;
				default:
					flag=false;
					break;
				}
			}
			if(flag){
				view[l]=node.getNext().getData();
				l++;
			}
			node=node.getNext();
		}
		if(l==0)return null;
		else return view;
	}

	@Override
	public Customer[] sort(String filed) {
		// TODO Auto-generated method stub
		Customer[] view=new Customer[db.getSize()];
		Customer temp;
		MyLinkedList<Customer>.Node<Customer> node =db.getHead();
		for (int i = 0; i < view.length; i++) {
			view[i]=node.getNext().getData();
			node=node.getNext();
		}
		for(int i=0;i<view.length-1;i++){
			for (int j = 0; j < view.length-1; j++) {
				switch (filed) {
				case "id":
					if (view[j].getId().compareTo(view[j+1].getId())>0) {
						temp=view[j];
						view[j]=view[j+1];
						view[j+1]=temp;
					}
					break;
				case "name":
					if (view[j].getName().compareTo(view[j+1].getName())>0) {
						temp=view[j];
						view[j]=view[j+1];
						view[j+1]=temp;
					}
					break;
				case "sex":
					if (view[j].getSex().compareTo(view[j+1].getSex())>0) {
						temp=view[j];
						view[j]=view[j+1];
						view[j+1]=temp;
					}
					break;
				case "address":
					if (view[j].getAddress().compareTo(view[j+1].getAddress())>0) {
						temp=view[j];
						view[j]=view[j+1];
						view[j+1]=temp;
					}
					break;
				case "birthday":
					if (view[j].getBirthday().compareTo(view[j+1].getBirthday())>0) {
						temp=view[j];
						view[j]=view[j+1];
						view[j+1]=temp;
					}
					break;
				case "call":
					if (view[j].getCall().compareTo(view[j+1].getCall())>0) {
						temp=view[j];
						view[j]=view[j+1];
						view[j+1]=temp;
					}
					break;
				default:
					break;
				}
			}
		}
		return view;
	}
	
	public Customer findCustomerById(String id){
		MyLinkedList<Customer>.Node<Customer> node;
		node=db.getHead();
		while(node.getNext()!=null){
			if(node.getNext().getData().getId().compareTo(id)==0){
				return node.getNext().getData();
			}
			node=node.getNext();
		}
		return null;
	}
	
	@Override
	public Customer[] allCustomers(){
		Customer[] view=new Customer[db.getSize()];
		MyLinkedList<Customer>.Node<Customer> node = db.getHead();
		for (int i = 0; i < view.length; i++) {
			view[i]=node.getNext().getData();
			node=node.getNext();
		}
		return view;
	}
	
	@Override
	public Customer cloneCustomer(String id){
		Customer clone = new Customer();
		Customer customer = findCustomerById(id);
		if(customer==null)return null;
		clone.setId(customer.getId());
		clone.setName(customer.getName());
		clone.setAddress(customer.getAddress());
		clone.setBirthday(customer.getBirthday());
		clone.setSex(customer.getSex());
		clone.setCall(customer.getCall());
		return clone;
	}
	
}
